﻿using System;

namespace Nelibur.ObjectMapper.CodeGenerators.Emitters
{
    internal sealed class EmitTryCatchType : IEmitterType
    {
        private readonly IEmitterType emitter;
        private readonly string errorInfo;

        private EmitTryCatchType(IEmitterType emitter, string errorInfo)
        {
            this.emitter = emitter;
            this.errorInfo = errorInfo;
        }
        public void Emit(CodeGenerator generator)
        {
            generator.BeginTry();
            emitter.Emit(generator);
            generator.BeginCatch(errorInfo);
        }

        public Type ObjectType
        {
            get { return emitter.ObjectType; }
        }
        public static IEmitterType TryCatch(IEmitterType emitter, string errorInfo)
        {
            return new EmitTryCatchType(emitter, errorInfo);
        }
    }
}